package com.rzico.sbl.ui.report.dispatch

import android.os.Bundle
import android.view.LayoutInflater
import android.view.ViewGroup
import android.widget.EditText
import com.lzg.extend.jackson.JsonUtil
import com.rzico.sbl.R
import com.rzico.sbl.databinding.FragmentDispatchRottenBinding
import com.rzico.sbl.model.DispatchRotten
import com.rzico.sbl.viewmodel.ReportDispatchViewModel
import com.xinnuo.common.extend.formatDecimal
import com.xinnuo.common.extend.get
import com.xinnuo.common.decoration.SpaceTBDecoration
import com.xinnuo.common_ui.base.NavigationFragment
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.bindLifeCycle
import com.xinnuo.common_ui.utils.subscribeBySuccess
import com.xinnuo.slimadapter.SlimAdapter
import org.json.JSONArray
import org.json.JSONObject

class DispatchRottenFragment
@JvmOverloads
constructor(
    private val shippingSn: String = ""
) : NavigationFragment() {

    private var mBinding: FragmentDispatchRottenBinding? = null

    override fun getViewModel() = get<ReportDispatchViewModel>()

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ) = FragmentDispatchRottenBinding
        .inflate(inflater, container, false)
        .also { mBinding = it }
        .root

    override fun initTitle() {
        initLayout()

        mBinding?.let {
            it.listLayout
                .swipeRefresh
                .isRefreshing = true

            getData()
        }
    }

    private fun initLayout() {
        mBinding?.let { binding ->
            binding.listLayout.apply {
                recycleList.also { layout ->
                    layout.loadLinear(
                        refresh = swipeRefresh,
                        onRefresh = { getData() }
                    )

                    layout.addItemDecoration(SpaceTBDecoration(hasHeader = false))

                    mAdapter = SlimAdapter.creator()
                        .register<DispatchRotten>(R.layout.item_report_dispatch_rotten) { jector, bean, _ ->
                            jector.apply {
                                gone(R.id.item_rotten_del)
                                text(R.id.item_rotten_name, bean.name)
                                text(R.id.item_rotten_price, bean.price.formatDecimal())

                                // 烂桶数量
                                with<EditText>(R.id.item_rotten_count) {
                                    it.isEnabled = false
                                    it.setText(bean.quantity)
                                }

                                // 烂桶金额
                                with<EditText>(R.id.item_rotten_amount) {
                                    it.isEnabled = false
                                    it.setText(bean.subPrice.formatDecimal())
                                }
                            }
                        }
                        .attachTo(layout)
                }
            }
        }
    }

    /**
     * 收款列表
     */
    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .dispatchPay(
                shippingSn = shippingSn,
                isLoading = false
            )
            .bindLifeCycle(getLifecycleOwner())
            .subscribeBySuccess(
                event = getViewModel(),
                onSuccess = { result ->
                    val obj = JSONObject(result)

                    val objItems = obj.optJSONArray("shippingSwaps") ?: JSONArray()
                    mList.clear()
                    mList.addItems(
                        JsonUtil.toList(
                            objItems.toString(),
                            DispatchRotten::class.java
                        )
                    )

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    mBinding?.let {
                        it.listLayout
                            .swipeRefresh
                            .isRefreshing = false
                    }
                }
            )
    }

    override fun onDestroyView() {
        super.onDestroyView()
        mBinding = null
    }

    companion object {
        fun newInstance(shippingSn: String) = DispatchRottenFragment(shippingSn)
    }

}